home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Formats / 5.0spec next >
Encoding:
Text File  |  1992-09-23  |  102.0 KB  |  2,282 lines

  1.  
  2.           An Aldus/Microsoft Technical Memorandum:  8/8/88    Page 1
  3.           
  4.           
  5.           
  6.           Preface
  7.           
  8.           
  9.           This memorandum  has been prepared jointly by Aldus and Microsoft
  10.           in conjunction  with leading scanner vendors and other interested
  11.           parties.   This document  does not  represent a commitment on the
  12.           part of  either Microsoft  or Aldus  to provide  support for this
  13.           file format  in any  application.   When responding  to  specific
  14.           issues raised  in this memo, or when requesting additional tag or
  15.           field assignments, please address your correspondence to either:
  16.           
  17.                Developers' Desk    Windows Marketing Group
  18.                Aldus Corporation   Microsoft Corporation
  19.                411 First Ave. South     16011 NE 36th Way
  20.                Suite 200 Box 97017
  21.                Seattle, WA  98104  Redmond, WA  98073-9717
  22.                (206) 622-5500 (206) 882-8080
  23.           
  24.           
  25.           
  26.           Revision Notes
  27.           
  28.           This revision  replaces "TIFF  Revision 4."   Sections in italics
  29.           are new or substantially changed in this revision.  Also new, but
  30.           not in italics, are Appendices F, G, and H.
  31.           
  32.           The major enhancements in TIFF 5.0 are:
  33.           
  34.           1.   Compression of  grayscale and  color images, for better disk
  35.           space utilization.  See Appendix F.
  36.           
  37.           2.   TIFF Classes - restricted TIFF subsets that can simplify the
  38.           job of  the TIFF  implementor.   You may  wish to scan Appendix G
  39.           before reading the rest of this document.   In fact, you may want
  40.           to use  Appendix G as your main guide, and refer back to the main
  41.           body of  the specification  as needed for details concerning TIFF
  42.           structures and field definitions.
  43.           
  44.           3.   Support for  "palette color"   images.  See the TIFF Class P
  45.           description  in   Appendix  G,   and  the   new  ColorMap   field
  46.           description.
  47.           
  48.           4.   Two new  tags that  can be  used to  more fully  define  the
  49.           characteristics of  full color  RGB data, and thereby potentially
  50.           improve the quality of color image reproduction.  See Appendix H.
  51.           
  52.           The organization  of the  document has also changed slightly.  In
  53.           particular, the  tags are  listed in  alphabetical order,  within
  54.           several categories, in the main body of the specification.
  55.           
  56.           As always,  every attempt  has been  made to add functionality in
  57.           such a  way as  to minimize  incompatibility problems  with older
  58.           TIFF software.   In  particular, many  TIFF  5.0  files  will  be
  59.           readable even  by older  applications that  assume TIFF 4.0 or an
  60.           earlier version  of the  specification.   One exception  is  with
  61.           files that  use the  new TIFF  5.0 LZW  compression scheme.   Old
  62.           applications will  have to  give up  in this case, of course, and
  63.           will do so "gracefully" if they have been following the rules.
  64.           
  65.           We  are  grateful  to  all  of  the  draft  reviewers  for  their
  66.           suggestions.   Especially helpful  were Herb  Weiner  of  Kitchen
  67.           Wisdom  Publishing   Company,  Brad  Pillow  of  TrueVision,  and
  68.           engineers from Hewlett Packard and Quark.  Chris Sears of Magenta
  69.           Graphics provided information which is included as Appendix H.
  70.           
  71.           
  72.           Abstract
  73.           
  74.           This document  describes TIFF,  a tag  based file  format that is
  75.           designed to promote the interchange of digital image data.
  76.           
  77.           The fields  were defined  primarily with  desktop publishing  and
  78.           related applications  in mind, although it is possible that other
  79.           sorts of imaging applications may find TIFF to be useful.
  80.           
  81.           The general  scenario for  which TIFF  was invented  assumes that
  82.           applications software  for scanning  or painting  creates a  TIFF
  83.           file, which  can then  be read and incorporated into a "document"
  84.           or "publication"   by an application such as a desktop publishing
  85.           package.
  86.           
  87.           TIFF is  not a printer language or page description language, nor
  88.           is it intended to be a general document interchange standard. The
  89.           primary design  goal was  to provide  a rich  environment  within
  90.           which the exchange of image data between application programs can
  91.           be accomplished.   This  richness is  required in  order to  take
  92.           advantage of  the varying  capabilities of  scanners and  similar
  93.           devices.  TIFF is therefore designed to be a superset of existing
  94.           image file  formats for  "desktop"   scanners (and paint programs
  95.           and anything  else that  produces images with pixels in them) and
  96.           will be enhanced on a continuing basis as new capabilities arise.
  97.           A high  priority has been given to structuring the data in such a
  98.           way as  to minimize  the pain  of future  additions.    TIFF  was
  99.           designed to be an extensible interchange format.
  100.           
  101.           Although TIFF  is claimed  to be  in some sense a rich format, it
  102.           can easily  be used for simple scanners and applications as well,
  103.           since the  application developer  need only be concerned with the
  104.           capabilities that he requires.
  105.           
  106.           TIFF is intended to be independent of specific operating systems,
  107.           filing systems,  compilers, and processors.  The only significant
  108.           assumption is  that the  storage medium supports something like a
  109.           "file,"   defined as  a sequence  of 8-bit bytes, where the bytes
  110.           are numbered  from 0  to N.   The  largest possible  TIFF file is
  111.           2**32 bytes  in length.   Since TIFF uses pointers (byte offsets)
  112.           quite liberally,  a TIFF  file is  most easily read from a random
  113.           access device  such as a hard disk or flexible diskette, although
  114.           it should  be possible  to read  and write TIFF files on magnetic
  115.           tape.
  116.           
  117.           The recommended  MS-DOS, UNIX,  and OS/2  file extension for TIFF
  118.           files is  ".TIF."   The recommended Macintosh filetype is "TIFF".
  119.           Suggestions for  conventions in  other computing environments are
  120.           welcome.
  121.           
  122.           
  123.           1) Structure
  124.           
  125.           In TIFF, individual fields are identified with a unique tag. This
  126.           allows particular fields to be present or absent from the file as
  127.           required by the application.  For an explanation of the rationale
  128.           behind using a tag structure format, see Appendix A.
  129.           
  130.           A TIFF file begins with an 8-byte "image file header" that points
  131.           to one  or  more  "image  file  directories."    The  image  file
  132.           directories contain  information about  the images,  as  well  as
  133.           pointers to the actual image data.
  134.           
  135.           See Figure 1.
  136.           
  137.           We will now describe these structures in more detail.
  138.           
  139.           Image file header
  140.           
  141.           A TIFF  file begins  with an 8-byte image file header, containing
  142.           the following information:
  143.           
  144.           Bytes 0-1:     The first  word of  the file  specifies  the  byte
  145.           order used within the file.  Legal values are:
  146.           
  147.                     "II" (hex 4949)
  148.                     "MM" (hex 4D4D)
  149.           
  150.                In the  "II"   format,  byte  order  is  always  from  least
  151.           significant to  most significant,  for  both  16-bit  and  32-bit
  152.           integers.   In the  "MM"   format, byte order is always from most
  153.           significant to  least significant,  for both  16-bit  and  32-bit
  154.           integers.   In both  formats, character  strings are  stored into
  155.           sequential byte locations.
  156.           
  157.                All  TIFF  readers should  support  both  byte  orders - see
  158.           Appendix G.
  159.           
  160.           Bytes 2-3 The second  word of  the  file  is  the  TIFF  "version
  161.           number."   This number, 42 (2A in hex), is not to be equated with
  162.           the current  Revision of  the TIFF  specification.   In fact, the
  163.           TIFF version  number (42)  has never  changed, and probably never
  164.           will.   If it  ever does,  it means that TIFF has changed in some
  165.           way so  radical that  a TIFF  reader should  give up immediately.
  166.           The number 42 was chosen for its deep philosophical significance.
  167.           It can and should be used as additional verification that this is
  168.           indeed a TIFF file.
  169.           
  170.                A TIFF  file does  not have  a real version/revision number.
  171.           This was  an explicit,  conscious design  decision.  In many file
  172.           formats, fields  take on different meanings depending on a single
  173.           version number.   The  problem is that as the file format "ages,"
  174.           it becomes  increasingly difficult  to document which fields mean
  175.           what in  a given  version, and older software usually has to give
  176.           up if  it encounters  a file  with a  newer version  number.   We
  177.           wanted TIFF  fields to have a permanent and well-defined meaning,
  178.           so that  "older" software  can usually  read "newer"  TIFF files.
  179.           The bottom line is lower software release costs and more reliable
  180.           software.
  181.           
  182.           Bytes 4-7 This long  word contains  the offset  (in bytes) of the
  183.           first Image File Directory.  The directory may be at any location
  184.           in the  file after  the header but must begin on a word boundary.
  185.           In particular,  an Image File Directory may follow the image data
  186.           it describes.   Readers must simply follow the pointers, wherever
  187.           they may lead.
  188.           
  189.                (The term  "byte offset"  is always used in this document to
  190.           refer to  a location  with respect  to the beginning of the file.
  191.           The first byte of the file has an offset of 0.)
  192.           
  193.           
  194.           Image file directory
  195.           
  196.           An Image  File Directory  (IFD) consists of a 2-byte count of the
  197.           number of  entries (i.e.,  the number  of fields),  followed by a
  198.           sequence of 12-byte field entries, followed by a 4-byte offset of
  199.           the next  Image File  Directory (or 0 if none).  Do not forget to
  200.           write the 4 bytes of 0 after the last IFD.
  201.           
  202.           Each 12-byte IFD entry has the following format:
  203.           
  204.           Bytes 0-1 contain the Tag for the field.
  205.           Bytes 2-3 contain the field Type.
  206.           Bytes 4-7 contain the  Length ("Count"  might have  been a better
  207.           term) of the field.
  208.           Bytes 8-11     contain the  Value Offset,  the  file  offset  (in
  209.           bytes) of  the Value  for the  field.   The Value  is expected to
  210.           begin on  a word  boundary; the  corresponding Value  Offset will
  211.           thus be  an even  number.  This file offset may point to anywhere
  212.           in the file, including after the image data.
  213.           
  214.           The entries  in an  IFD must be sorted in ascending order by Tag.
  215.           Note that this is not the order in which the fields are described
  216.           in this  document.   For a  numerically ordered list of tags, see
  217.           Appendix E.  The Values to which directory entries point need not
  218.           be in any particular order in the file.
  219.           
  220.           In order  to save time and space, the Value Offset is interpreted
  221.           to contain  the Value  instead of  pointing to  the Value  if the
  222.           Value fits  into 4  bytes.  If the Value is less than 4 bytes, it
  223.           is left-justified within the 4-byte Value Offset, i.e., stored in
  224.           the lower-numbered bytes.  Whether or not the Value fits within 4
  225.           bytes is  determined by  looking at  the Type  and Length  of the
  226.           field.
  227.           
  228.           The Length  is specified in terms of the data type, not the total
  229.           number of bytes.  A single 16-bit word (SHORT) has a Length of 1,
  230.           not 2,  for example.   The  data  types  and  their  lengths  are
  231.           described below:
  232.           
  233.           1 = BYTE  An 8-bit unsigned integer.
  234.           2 = ASCII 8-bit bytes  that store ASCII codes; the last byte must
  235.           be null.
  236.           3 = SHORT A 16-bit (2-byte) unsigned integer.
  237.           4 = LONG  A 32-bit (4-byte) unsigned integer.
  238.           5 = RATIONAL   Two LONG's:  the first represents the numerator of
  239.           a fraction, the second the denominator.
  240.           
  241.           The value of the Length part of an ASCII field entry includes the
  242.           null.   If padding  is necessary, the Length does not include the
  243.           pad byte.   Note  that there  is no  "count byte," as there is in
  244.           Pascal-type strings.   The Length part of the field takes care of
  245.           that.   The null  is not  strictly necessary, but may make things
  246.           slightly simpler for C programmers.
  247.           
  248.           The reader  should check  the type  to ensure  that it is what he
  249.           expects.   TIFF currently  allows more than 1 valid type for some
  250.           fields.   For example,  ImageWidth and ImageLength were specified
  251.           as having  type SHORT.  Very large images with more than 64K rows
  252.           or columns  are possible with some devices even now.  Rather than
  253.           add parallel  LONG tags  for these fields, it is cleaner to allow
  254.           both SHORT  and LONG  for ImageWidth  and similar  fields.    See
  255.           Appendix G for specific recommendations.
  256.           
  257.           Note that  there may  be more  than one IFD.  Each IFD is said to
  258.           define a "subfile."   One potential use of subsequent subfiles is
  259.           to describe  a "sub-image"   that  is somehow related to the main
  260.           image, such as a reduced resolution version of the image.
  261.           
  262.           If you have not already done so, you may wish to turn to Appendix
  263.           G to study the sample TIFF images.
  264.           
  265.           
  266.           2) Definitions
  267.           
  268.           Note that the TIFF structure as described in the previous section
  269.           is not  specific to  imaging applications in any way.  It is only
  270.  
  271.           the definitions of the fields themselves that jointly describe an
  272.           image.
  273.           
  274.           Before we  begin defining  the fields,  we will define some basic
  275.           concepts.   An image  is defined  to be  a rectangular  array  of
  276.           "pixels,"  each of which consists of one or more "samples."  With
  277.           monochromatic data,  we have  one sample  per pixel, and "sample"
  278.           and "pixel"   can  be  used  interchangeably.    RGB  color  data
  279.           contains three samples per pixel.
  280.           
  281.           
  282.           3) The Fields
  283.           
  284.           This section  describes the  fields defined  in this  version  of
  285.           TIFF.   More fields may be added in future versions - if possible
  286.           they will  be added in such a way so as not to break old software
  287.           that encounters a newer TIFF file.
  288.           
  289.           The documentation  for each  field contains the name of the field
  290.           (quite arbitrary, but convenient), the Tag value, the field Type,
  291.           the Number of Values (N) expected, comments describing the field,
  292.           and the  default, if  any.  Readers must assume the default value
  293.           if the field does not exist.
  294.           
  295.           "No default"  does not  mean that  a TIFF  writer should  not pay
  296.           attention to  the tag.  It simply means that there is no default.
  297.           If the  writer has reason to believe that readers will care about
  298.           the value  of this  field, the writer should write the field with
  299.           the appropriate value.  TIFF readers can do whatever they want if
  300.           they encounter a missing "no default" field that they care about,
  301.           short of  refusing to  import the file.  For example, if a writer
  302.           does  not  write  out  a  PhotometricInterpretation  field,  some
  303.           applications will  interpret the  image "correctly,"  and  others
  304.           will display  the image  inverted.  This is not a good situation,
  305.           and writers should be careful not to let it happen.
  306.           
  307.           The  fields   are  grouped   into  several  categories:    basic,
  308.           informational, facsimile,  document storage and retrieval, and no
  309.           longer recommended.   A  future version  of the specification may
  310.           pull some of these categories into separate companion documents.
  311.           
  312.           Many fields  are described  in this  document, but  most are  not
  313.           "required."   See Appendix  G for  a list  of required fields, as
  314.           well as  examples of  how to combine fields into valid and useful
  315.           TIFF files.
  316.           Basic Fields
  317.           
  318.           
  319.           Basic fields  are  fields  that  are  fundamental  to  the  pixel
  320.           architecture or visual characteristics of an image.
  321.           
  322.           BitsPerSample
  323.           Tag  = 258  (102)
  324.           Type = SHORT
  325.           N    = SamplesPerPixel
  326.           
  327.           Number of bits per sample.  Note that this tag allows a different
  328.           number of  bits per  sample for  each sample  corresponding to  a
  329.           pixel.   For example, RGB color data could use a different number
  330.           of bits  per sample for each of the three color planes.  Most RGB
  331.           files will have the same number of BitsPerSample for each sample.
  332.           Even in this case, be sure to include all three entries.  Writing
  333.           "8" when you mean "8,8,8" sets a bad precedent for other fields.
  334.           
  335.           Default = 1.  See also SamplesPerPixel.
  336.           
  337.           
  338.           ColorMap
  339.           Tag  = 320 (140)
  340.           Type = SHORT
  341.           N    = 3 * (2**BitsPerSample)
  342.           
  343.           This tag  defines a  Red-Green-Blue color  map for  palette color
  344.           images.   The palette color pixel value is used to index into all
  345.           3 subcurves.   For  example, a Palette color pixel having a value
  346.           of 0  would be  displayed according  to the 0th entry of the Red,
  347.           Green, and Blue subcurves.
  348.           
  349.           The subcurves  are stored  sequentially.   The Red  entries  come
  350.           first, followed  by the  Green  entries,  followed  by  the  Blue
  351.           entries.   The length  of each  subcurve is  2**BitsPerSample.  A
  352.           ColorMap entry  for an  8-bit Palette color image would therefore
  353.           have 3  * 256  entries.   The width  of each entry is 16 bits, as
  354.           implied  by  the  type  of  SHORT.    0  represents  the  minimum
  355.           intensity, and  65535 represents the maximum intensity.  Black is
  356.           represented by  0,0,0, and  white by  65535, 65535,  65535.   The
  357.           purpose of  the color  map is to act as a "lookup"  table mapping
  358.           pixel values from 0 to 2**BitsPerSample-1 into RGB triplets.
  359.           
  360.           The ColorResponseCurves  field may  be used  in conjunction  with
  361.           ColorMap to further refine the meaning of the RGB triplets in the
  362.           ColorMap.   However, the  ColorResponseCurves default  should  be
  363.           sufficient in most cases.
  364.           
  365.           See also PhotometricInterpretation - palette color.
  366.           
  367.           No default.   ColorMap  must be  included in  all  palette  color
  368.           images.
  369.           
  370.           
  371.           ColorResponseCurves
  372.           Tag  = 301 (12D)
  373.           Type = SHORT
  374.           N    = 3 * (2**BitsPerSample)
  375.           
  376.           This tag  defines three  color response curves, one each for Red,
  377.           Green and  Blue color  information.   The Red entries come first,
  378.           followed by the Green entries, followed by the Blue entries.  The
  379.           length  of   each  subcurve   is  2**BitsPerSample,   using   the
  380.           BitsPerSample value corresponding to the respective primary.  The
  381.           width of  each entry is 16 bits, as implied by the type of SHORT.
  382.           0 represents  the minimum  intensity, and  65535  represents  the
  383.           maximum intensity.   Black  is represented by 0,0,0, and white by
  384.           65535, 65535,  65535.   Therefore, a ColorResponseCurve entry for
  385.           RGB data  where each of the samples is 8 bits deep would have 3 *
  386.           256 entries, each consisting of a SHORT.
  387.           
  388.           The purpose of the color response curves is to refine the content
  389.           of RGB color images.
  390.           
  391.           See Appendix H, section VII, for further information.
  392.           
  393.           Default:  curves based on the NTSC recommended gamma of 2.2.
  394.           
  395.           
  396.           Compression
  397.           Tag  = 259  (103)
  398.           Type = SHORT
  399.           N    = 1
  400.           
  401.           1 =  No compression,  but pack  data into  bytes  as  tightly  as
  402.           possible, with  no unused  bits except  at the end of a row.  The
  403.           bytes are  stored as  an array of type BYTE, for BitsPerSample <=
  404.           8,  SHORT   if  BitsPerSample   >  8  and  <=  16,  and  LONG  if
  405.           BitsPerSample >  16 and <= 32.  The byte ordering of data >8 bits
  406.           must be  consistent with  that specified  in the TIFF file header
  407.           (bytes 0  and 1).   "II"    format  files  will  have  the  least
  408.           significant bytes  preceeding the  most significant  bytes  while
  409.           "MM"  format files will have the opposite.
  410.           
  411.                If the  number of  bits per  sample is not a power of 2, and
  412.           you are willing to give up some space for better performance, you
  413.           may wish to use the next higher power of 2.  For example, if your
  414.           data can  be represented  in 6 bits, you may wish to specify that
  415.           it is 8 bits deep.
  416.           
  417.                Rows are  required to  begin on byte boundaries.  The number
  418.           of bytes  per row  is therefore  (ImageWidth *  SamplesPerPixel *
  419.           BitsPerSample  +   7)  /  8,  assuming  integer  arithmetic,  for
  420.           PlanarConfiguration=1.     Bytes  per   row  is   (ImageWidth   *
  421.           BitsPerSample + 7) / 8 for PlanarConfiguration=2.
  422.           
  423.                Some graphics  systems want rows to be word- or double-word-
  424.           aligned.   Uncompressed TIFF  rows will  need to  be copied  into
  425.           word- or  double-word-padded row  buffers before  being passed to
  426.           the graphics routines in these environments.
  427.           
  428.           2 =  CCITT Group  3 1-Dimensional  Modified  Huffman  run  length
  429.           encoding.   See Appendix  B:   "Data Compression  --  Scheme  2."
  430.           BitsPerSample must  be 1,  since  this  type  of  compression  is
  431.           defined only for bilevel images.
  432.           
  433.                When  you  decompress  data  that  has  been  compressed  by
  434.           Compression=2, you  must translate  white runs into 0's and black
  435.           runs into  1's.   Therefore, the normal PhotometricInterpretation
  436.           for those  compression types  is 0  (WhiteIsZero).   If a  reader
  437.           encounters a  PhotometricInterpretation of  1  (BlackIsZero)  for
  438.           such an  image, the  image should  be displayed  and printed with
  439.           black and white reversed.
  440.           
  441.           5 = LZW Compression,  for grayscale, mapped color, and full color
  442.           images.  See Appendix F.
  443.           
  444.           32773 =  PackBits compression,  a simple byte oriented run length
  445.           scheme for 1-bit images.  See Appendix C.
  446.           
  447.           Data compression only applies to raster image data, as pointed to
  448.           by StripOffsets.  All other TIFF information is unaffected.
  449.           
  450.           Default = 1.
  451.           
  452.           
  453.           GrayResponseCurve
  454.           Tag  = 291 (123)
  455.           Type = SHORT
  456.           N    = 2**BitsPerSample
  457.           
  458.           The purpose  of the  gray response curve and the gray units is to
  459.           provide more  exact photometric  interpretation  information  for
  460.           gray scale image data, in terms of optical density.
  461.           
  462.           The  GrayScaleResponseUnits   specifies  the   accuracy  of   the
  463.           information contained  in the  curve.   Since optical  density is
  464.           specified in  terms of  fractional numbers, this tag is necessary
  465.           to know  how to  interpret the  stored integer  information.  For
  466.           example, if  GrayScaleResponseUnits is  set to 4 (ten-thousandths
  467.           of a  unit), and a GrayScaleResponseCurve number for gray level 4
  468.           is 3455,  then the  resulting actual  value is  0.3455.   Optical
  469.           densitometers typically measure densities within the range of 0.0
  470.           to 2.0.
  471.           
  472.           If the  gray scale  response curve  is known  for the data in the
  473.           TIFF file, and if the gray scale response of the output device is
  474.           known, then  an intelligent  conversion can  be made  between the
  475.           input data and the output device.  For example, the output can be
  476.           made to  look just  like the  input.   In addition,  if the input
  477.           image lacks  contrast (as  can be  seen from the response curve),
  478.           then appropriate contrast enhancements can be made.
  479.           
  480.           The purpose  of the  gray scale  response curve  is to  act as  a
  481.           "lookup"   table mapping values from 0 to 2**BitsPerSample-1 into
  482.           specific   density    values.      The   0th   element   of   the
  483.           GrayResponseCurve array  is used to define the gray value for all
  484.           pixels  having   a  value   of  0,   the  1st   element  of   the
  485.           GrayResponseCurve array  is used to define the gray value for all
  486.           pixels having  a value of 1, and so on, up to 2**BitsPerSample-1.
  487.           If your  data is  "really," say, 7-bit data, but you are adding a
  488.           1-bit pad  to each  pixel to  turn it into 8-bit data, everything
  489.           still works:   If  the data is high-order justified, half of your
  490.           GrayResponseCurve entries  (the odd ones, probably) will never be
  491.           used, but  that doesn't  hurt anything.  If the data is low-order
  492.           justified, your  pixel values  will be between 0 and 127, so make
  493.           your GrayResponseCurve  accordingly.   What your  curve does from
  494.           128 to  255 doesnt matter.  Note that low-order justification is
  495.           probably not  a good  idea, however,  since not  all applications
  496.           look at GrayResponseCurve.  Note also that LZW compression yields
  497.           the same  compression ratio  regardless of  whether the  data  is
  498.           high-order or low-order justified.
  499.           
  500.           It is  permissable to  have a  GrayResponseCurve even for bilevel
  501.           (1-bit) images.   The  GrayResponseCurve will  have 2 values.  It
  502.           should be noted, however, that TIFF B readers are not required to
  503.           pay attention  to  GrayResponseCurves  in  TIFF  B  files.    See
  504.           Appendix G.
  505.           
  506.           If both  GrayResponseCurve and  PhotometricInterpretation  fields
  507.           exist  in   the  IFD,   GrayResponseCurve  values   override  the
  508.           PhotometricInterpretation value.   But it is a good idea to write
  509.           out both, since some applications do not yet pay attention to the
  510.           GrayResponseCurve.
  511.           
  512.           Writers may  wish to  purchase a  Kodak Reflection Density Guide,
  513.           catalog number  146 5947,  available for  $10 or  so at  prepress
  514.           supply houses,  to help them figure out reasonable density values
  515.           for their scanner or frame grabber.  If that sounds like too much
  516.           work,   we    recommend   a    curve   that    is    linear    in
  517.           intensity/reflectance.  To compute reflectance from density:  R =
  518.           1 /  pow(10,D).   To compute  density from reflectance: D = log10
  519.           (1/R).   A typical  4-bit GrayResponseCurve  may  look  therefore
  520.           something like:   2000,  1177, 875, 699, 574, 477, 398, 331, 273,
  521.           222, 176,  135, 97, 62, 30, 0, assuming GrayResponseUnit=3.  Such
  522.           a curve would be consistent with PhotometricInterpretation=1.
  523.           
  524.           See also GrayResponseUnit, PhotometricInterpretation, ColorMap.
  525.           
  526.           
  527.           GrayResponseUnit
  528.           Tag  = 290 (122)
  529.           Type = SHORT
  530.           N    = 1
  531.           
  532.           1 = Number represents tenths of a unit.
  533.           2 = Number represents hundredths of a unit.
  534.           3 = Number represents thousandths of a unit.
  535.           4 = Number represents ten-thousandths of a unit.
  536.           5 = Number represents hundred-thousandths of a unit.
  537.           
  538.           Modifies GrayResponseCurve.
  539.           
  540.           See also GrayResponseCurve.
  541.           
  542.           For historical  reasons, the  default is 2.  However, for greater
  543.           accuracy, we recommend using 3.
  544.           
  545.           
  546.           ImageLength
  547.           Tag  = 257  (101)
  548.           Type = SHORT or LONG
  549.           N    = 1
  550.           
  551.           The image's  length (height) in pixels (Y: vertical).  The number
  552.           of rows  (sometimes described as "scan lines") in the image.  See
  553.           also ImageWidth.
  554.           
  555.           No default.
  556.           
  557.           
  558.           ImageWidth
  559.           Tag  = 256  (100)
  560.           Type = SHORT or LONG
  561.           N    = 1
  562.           
  563.           The image's  width, in  pixels (X:  horizontal).   The number  of
  564.           columns in the image.  See also ImageLength.
  565.           
  566.           No default.
  567.           
  568.           
  569.           NewSubfileType
  570.           Tag =  254  (FE)
  571.           Type = LONG
  572.           N = 1
  573.           
  574.           Replaces the  old SubfileType  field, due  to limitations  in the
  575.           definition of that field.
  576.           
  577.           A general  indication of  the kind  of data  that is contained in
  578.           this subfile.   This  field is  made up of a set of 32 flag bits.
  579.           Unused bits are expected to be 0.  Bit 0 is the low-order bit.
  580.           
  581.           Currently defined values are:
  582.           
  583.           Bit 0     is 1  if the  image is  a reduced resolution version of
  584.           another image in this TIFF file; else the bit is 0.
  585.           Bit 1     is 1  if the  image is  a single  page of  a multi-page
  586.           image (see the PageNumber tag description); else the bit is 0.
  587.           Bit 2     is 1  if the  image defines  a  transparency  mask  for
  588.           another image  in this  TIFF file.  The PhotometricInterpretation
  589.           value must be 4, designating a transparency mask.
  590.           
  591.           These values  have been  defined as  bit flags  because they  are
  592.           pretty much independent of each other.  For example, it be useful
  593.           to have  four images  in a  single TIFF  file: a  full resolution
  594.           image, a  reduced resolution  image, a  transparency mask for the
  595.           full resolution  image, and  a transparency  mask for the reduced
  596.           resolution image.  Each of the four images would have a different
  597.           value for the NewSubfileType field.
  598.           
  599.           Default is 0.
  600.           
  601.           
  602.           PhotometricInterpretation
  603.           Tag  = 262  (106)
  604.           Type = SHORT
  605.           N    = 1
  606.           
  607.           0 =  For bilevel  and grayscale  images:   0 is  imaged as white.
  608.           2**BitsPerSample-1 is  imaged as  black.    If  GrayResponseCurve
  609.           exists,  it   overrides  the   PhotometricInterpretation   value,
  610.           although  it  is  safer  to  make  them  match,  since  some  old
  611.           applications may still be ignoring GrayResponseCurve. This is the
  612.           normal value for Compression=2.
  613.           
  614.           1 =  For bilevel  and grayscale  images:   0 is  imaged as black.
  615.           2**BitsPerSample-1  is  imaged  as  white.  If  GrayResponseCurve
  616.           exists,  it   overrides  the   PhotometricInterpretation   value,
  617.           although  it  is  safer  to  make  them  match,  since  some  old
  618.           applications may  still be  ignoring GrayResponseCurve.  If  this
  619.           value is  specified for  Compression=2, the  image should display
  620.           and print reversed.
  621.           
  622.           2 = RGB.  In the RGB model, a color is described as a combination
  623.           of the  three primary  colors of  light (red, green, and blue) in
  624.           particular concentrations.   For  each of  the three  samples,  0
  625.           represents minimum intensity, and 2**BitsPerSample - 1 represents
  626.           maximum intensity.   Thus  an RGB  value  of  (0,0,0)  represents
  627.           black,  and   (255,255,255)  represents   white,  assuming  8-bit
  628.           samples.   For PlanarConfiguration = 1, the samples are stored in
  629.           the indicated  order:   first Red,  then Green,  then Blue.   For
  630.           PlanarConfiguration =  2, the  StripOffsets for the sample planes
  631.           are stored  in the  indicated order:   first the Red sample plane
  632.           StripOffsets, then  the Green  plane StripOffsets,  then the Blue
  633.           plane StripOffsets.
  634.           
  635.                The ColorResponseCurves field may be used to globally refine
  636.           or alter  the color  balance of  an RGB  image without  having to
  637.           change the values of the pixels themselves.
  638.           
  639.           3="Palette color."     In this  mode, a color is described with a
  640.           single sample.   The  sample is  used as  an index into ColorMap.
  641.           The sample  is used to index into each of the red, green and blue
  642.           curve tables to retrieve an RGB triplet defining an actual color.
  643.           When this  PhotometricInterpretation value  is  used,  the  color
  644.           response curves  must also  be supplied.  SamplesPerPixel must be
  645.           1.
  646.           
  647.           4 =  Transparency Mask.   This  means that  the image  is used to
  648.           define an  irregularly shaped region of another image in the same
  649.           TIFF  file.     SamplesPerPixel  and  BitsPerSample  must  be  1.
  650.           PackBits compression  is recommended.    The  1-bits  define  the
  651.           interior of  the region;  the 0-bits  define the  exterior of the
  652.           region.  The Transparency Mask must have the same ImageLength and
  653.           ImageWidth as the main image.
  654.           
  655.                A reader  application can  use the  mask to  determine which
  656.           parts of the image to display.  Main image pixels that correspond
  657.           to 1-bits  in the  transparency mask  are imaged to the screen or
  658.           printer, but  main image  pixels that correspond to 0-bits in the
  659.           mask are not displayed or printed.
  660.           
  661.                It is  possible to  generalize the  notion of a transparency
  662.           mask to  include partial  transparency, but  it is not clear that
  663.           such information would be useful to a desktop publishing program.
  664.           
  665.           No default.   That  means that  if you  care  if  your  image  is
  666.           displayed and  printed as  "normal" vs "inverted," you must write
  667.           out this  field.   Do not rely on applications defaulting to what
  668.           you want!   PhotometricInterpretation  =  1  is  recommended  for
  669.           bilevel (except  for Compression=2)  and grayscale images, due to
  670.           popular user  interfaces for changing the brightness and contrast
  671.           of images.
  672.           
  673.           
  674.           PlanarConfiguration
  675.           Tag  = 284  (11C)
  676.           Type = SHORT
  677.           N    = 1
  678.           
  679.           1 =  The sample values for each pixel are stored contiguously, so
  680.           that   there    is   a    single   image    plane.            See
  681.           PhotometricInterpretation to  determine the  order of the samples
  682.           within the  pixel data.   So,  for RGB  data, the  data is stored
  683.           RGBRGBRGB...and so on.
  684.           
  685.           2 =  The samples  are stored  in separate  "sample planes."   The
  686.           values in StripOffsets and StripByteCounts are then arranged as a
  687.           2-dimensional array, with SamplesPerPixel rows and StripsPerImage
  688.           columns.      (All of  the columns  for row  0 are  stored first,
  689.           followed   by    the   columns    of   row   1,   and   so   on.)
  690.           PhotometricInterpretation describes  the type  of  data  that  is
  691.           stored in  each sample  plane.   For example,  RGB data is stored
  692.           with the  Red samples  in one sample plane, the Green in another,
  693.           and the Blue in another.
  694.           
  695.           If SamplesPerPixel  is 1,  PlanarConfiguration is irrelevant, and
  696.           should not be included.
  697.           Default is 1.  See also BitsPerSample, SamplesPerPixel.
  698.           
  699.           
  700.           Predictor
  701.           Tag  = 317 (13D)
  702.           Type = SHORT
  703.           N    = 1
  704.           
  705.           To be used when Compression=5 (LZW).  See Appendix F.
  706.           
  707.           1 = No prediction scheme used before coding.
  708.           
  709.           Default is 1.
  710.           
  711.           
  712.           ResolutionUnit
  713.           Tag  = 296 (128)
  714.           Type = SHORT
  715.           N    = 1
  716.           
  717.           To be used with XResolution and YResolution.
  718.           
  719.           1 =  No absolute  unit of  measurement.  Used for images that may
  720.           have a  non-square  aspect  ratio,  but  no  meaningful  absolute
  721.           dimensions.   The drawback  of ResolutionUnit=1 is that different
  722.           applications will  import the  image at different sizes.  Even if
  723.           the decision  is quite  arbitrary, it might be better to use dots
  724.           per inch  or  dots  per  centimeter,  and  pick  XResolution  and
  725.           YResolution such that the aspect ratio is correct and the maximum
  726.           dimension of  the image is about four inches (the "four" is quite
  727.           arbitrary.)
  728.           2 = Inch.
  729.           3 = Centimeter.
  730.           
  731.           Default is 2.  See also XResolution, YResolution.
  732.           
  733.           
  734.           RowsPerStrip
  735.           Tag  = 278  (116)
  736.           Type = SHORT or LONG
  737.           N    = 1
  738.           
  739.           The number  of rows  per strip.  The image data is organized into
  740.           strips for  fast access  to individual  rows  when  the  data  is
  741.           compressed - though this field is valid even  if the  data is not
  742.           compressed.
  743.           
  744.           RowsPerStrip and  ImageLength together  tell  us  the  number  of
  745.           strips in  the entire  image.   The equation  is StripsPerImage =
  746.           (ImageLength + RowsPerStrip - 1) / RowsPerStrip, assuming integer
  747.           arithmetic.
  748.           
  749.           Note that  either SHORT  or LONG  values can  be used  to specify
  750.           RowsPerStrip.   SHORT values  may be  used for  small TIFF files.
  751.           It should  be noted,  however, that  earlier  TIFF  specification
  752.           revisions required  LONG values  and that  some software  may not
  753.           expect SHORT values.  See Appendix G for further recommendations.
  754.           
  755.           Default is  2**32 -  1, which  is effectively infinity.  That is,
  756.           the entire  image is  one strip.   We  do not  recommend a single
  757.           strip, however.   Choose  RowsPerStrip such  that each  strip  is
  758.           about 8K  bytes, even  if the  data is  not compressed,  since it
  759.           makes buffering  simpler for  readers.   The "8K"  part is pretty
  760.           arbitrary, but seems to work well.
  761.           
  762.           See also ImageLength, StripOffsets, StripByteCounts.
  763.           
  764.           
  765.           SamplesPerPixel
  766.           Tag  = 277  (115)
  767.           Type = SHORT
  768.           N    = 1
  769.           
  770.           The number  of samples  per pixel.    SamplesPerPixel  is  1  for
  771.           bilevel, grayscale, and palette color images.  SamplesPerPixel is
  772.           3 for RGB images.
  773.           
  774.           Default = 1.  See also BitsPerSample, PhotometricInterpretation.
  775.           
  776.           
  777.           StripByteCounts
  778.           Tag  = 279  (117)
  779.           Type = SHORT or LONG
  780.           N    = StripsPerImage for PlanarConfiguration equal to 1.
  781.                = SamplesPerPixel  * StripsPerImage  for PlanarConfiguration
  782.           equal to 2
  783.           
  784.           For each strip, the number of bytes in that strip.  The existence
  785.           of  this   field  greatly   simplifies  the  chore  of  buffering
  786.           compressed data, if the strip size is reasonable.
  787.           
  788.           No default.  See also StripOffsets, RowsPerStrip.
  789.           
  790.           
  791.           StripOffsets
  792.           Tag  = 273  (111)
  793.           Type = SHORT or LONG
  794.           N    = StripsPerImage for PlanarConfiguration equal to 1.
  795.                = SamplesPerPixel  * StripsPerImage  for PlanarConfiguration
  796.           equal to 2
  797.           
  798.           For each  strip, the  byte offset  of that  strip.  The offset is
  799.           specified with  respect to  the beginning of the TIFF file.  Note
  800.           that this  implies that  each strip has a location independent of
  801.           the locations  of other  strips.   This feature may be useful for
  802.           editing applications.  This field is the only way for a reader to
  803.           find the image data, and hence must exist.
  804.           
  805.           Note that  either SHORT or LONG values can be used to specify the
  806.           strip offsets.   SHORT  values  may be used for small TIFF files.
  807.           It should  be noted,  however, that  earlier TIFF  specifications
  808.           required LONG strip offsets and that some software may not expect
  809.           SHORT values.  See Appendix G for further recommendations.
  810.           
  811.           No default.  See also StripByteCounts, RowsPerStrip.
  812.           
  813.           
  814.           XResolution
  815.           Tag  = 282  (11A)
  816.           Type = RATIONAL
  817.           N    = 1
  818.           
  819.           The number of pixels per ResolutionUnit in the X direction, i.e.,
  820.           in the  ImageWidth direction.   It  is, of  course, not mandatory
  821.           that the  image be  actually printed  at the size implied by this
  822.           parameter.   It is  up to the application to use this information
  823.           as it wishes.
  824.           
  825.           No default.  See also YResolution, ResolutionUnit.
  826.           
  827.           
  828.           YResolution
  829.           Tag  = 283  (11B)
  830.           Type = RATIONAL
  831.           N    = 1
  832.           
  833.           The number of pixels per ResolutionUnit in the Y direction, i.e.,
  834.           in the ImageLength direction.
  835.           
  836.           No default.  See also XResolution, ResolutionUnit.
  837.           
  838.           
  839.           Informational Fields
  840.           
  841.           
  842.           Informational  fields   are  fields   that  can   provide  useful
  843.           information to  a user,  such as where the image came from.  Most
  844.           are ASCII  fields.   An application could have some sort of "More
  845.           Info..." dialog box to display such information.
  846.           
  847.           Artist
  848.           Tag  = 315  (13B)
  849.           Type = ASCII
  850.           
  851.           Person who created the image.
  852.           
  853.           If you need to attach a Copyright notice to an image, this is the
  854.           place to  do it.  In fact, you may wish to write out the contents
  855.           of the field immediately after the 8-byte TIFF header.  Just make
  856.           sure your  IFD and field pointers are set accordingly, and you're
  857.           all set.
  858.           
  859.           
  860.           DateTime
  861.           Tag  = 306  (132)
  862.           Type = ASCII
  863.           N    = 20
  864.           
  865.           Date and  time of  image creation.   Use  the format  "YYYY:MM:DD
  866.           HH:MM:SS", with hours on a 24-hour clock, and one space character
  867.           between the  date and  the time.    The  length  of  the  string,
  868.           including the null, is 20 bytes.
  869.           
  870.           
  871.           HostComputer
  872.           Tag  = 316  (13C)
  873.           Type = ASCII
  874.           
  875.           "ENIAC", or whatever.
  876.           
  877.           See also Make, Model, Software.
  878.           
  879.           
  880.           ImageDescription
  881.           Tag  = 270 (10E)
  882.           Type = ASCII
  883.           
  884.           For example,  a user  may wish  to attach a comment such as "1988
  885.           company picnic" to an image.
  886.           
  887.           It has  been suggested  that  this  is  what  the  newspaper  and
  888.           magazine industry calls a "slug."
  889.           
  890.           
  891.           Make
  892.           Tag  = 271  (10F)
  893.           Type = ASCII
  894.           
  895.           Manufacturer of the scanner, video digitizer, or whatever.
  896.           
  897.           See also Model, Software.
  898.           
  899.           
  900.           Model
  901.           Tag  = 272  (110)
  902.           Type = ASCII
  903.           
  904.           The  model  name/number  of  the  scanner,  video  digitizer,  or
  905.           whatever.
  906.           
  907.           This tag is intended for user information only.
  908.           
  909.           See also Make, Software.
  910.           
  911.           
  912.           Software
  913.           Tag  = 305  (131)
  914.           Type = ASCII
  915.           
  916.           Name and  release number of the software package that created the
  917.           image.
  918.           
  919.           This tag is intended for user information only.
  920.           
  921.           See also Make, Model.
  922.           
  923.           
  924.           Facsimile Fields
  925.           
  926.           
  927.           Facsimile fields  may be  useful if  you are  using TIFF to store
  928.           facsimile messages  in "raw"  form.  They are not recommended for
  929.           use in interchange with desktop publishing applications.
  930.           
  931.           Compression (a basic tag)
  932.           Tag  = 259  (103)
  933.           Type = SHORT
  934.           N    = 1
  935.           
  936.           3 =  Facsimile-compatible CCITT  Group 3, exactly as specified in
  937.           "Standardization of  Group 3  facsimile  apparatus  for  document
  938.           transmission,"   Recommendation T.4,  Volume VII, Fascicle VII.3,
  939.           Terminal Equipment  and Protocols  for  Telematic  Services,  The
  940.           International  Telegraph  and  Telephone  Consultative  Committee
  941.           (CCITT), Geneva,  1985, pages  16 through  31.   Each strip  must
  942.           begin on  a byte  boundary.   (But recall  that an image can be a
  943.           single strip.)   Rows  that are  not the first row of a strip are
  944.           not required  to begin on a byte boundary.  The data is stored as
  945.           bytes,  not words - byte-reversal  is   not  allowed.    See  the
  946.           Group3Options field for Group 3 options such as 1D vs 2D coding.
  947.           
  948.           4 =  Facsimile-compatible CCITT  Group 4, exactly as specified in
  949.           "Facsimile Coding  Schemes and Coding Control Functions for Group
  950.           4 Facsimile Apparatus,"  Recommendation T.6, Volume VII, Fascicle
  951.           VII.3, Terminal  Equipment and  Protocols for Telematic Services,
  952.           The International  Telegraph and Telephone Consultative Committee
  953.           (CCITT), Geneva,  1985, pages  40 through  48.   Each strip  must
  954.           begin on  a byte  boundary.  Rows that are not the first row of a
  955.           strip are  not required to begin on a byte boundary.  The data is
  956.           stored as  bytes, not  words.   See the  Group4Options field  for
  957.           Group 4 options.
  958.           
  959.           
  960.           Group3Options
  961.           Tag  = 292  (124)
  962.           Type = LONG
  963.           N    = 1
  964.           
  965.           See Compression=3.   This  field is  made up  of a set of 32 flag
  966.           bits.   Unused bits are expected to be 0.  Bit 0 is the low-order
  967.           bit.   It is probably not safe to try to read the file if any bit
  968.           of this field is set that you don't know the meaning of.
  969.           
  970.           Bit 0     is 1  for 2-dimensional  coding (else  1-dimensional is
  971.           assumed).   For 2-D  coding, if more than one strip is specified,
  972.           each strip  must begin  with a  1-dimensionally coded line.  That
  973.           is, RowsPerStrip  should be  a multiple  of  "Parameter  K"    as
  974.           documented in the CCITT specification.
  975.           
  976.           Bit 1     is 1 if uncompressed mode is used.
  977.           
  978.           Bit 2     is 1  if fill  bits have been added as necessary before
  979.           EOL codes  such that  EOL always  ends on  a byte  boundary, thus
  980.           ensuring an  eol-sequence of  a 1 byte preceded by a zero nibble:
  981.           xxxx-0000 0000-0001.
  982.           
  983.           Default  is   0,  for  basic  1-dimensional  coding.    See  also
  984.           Compression.
  985.           
  986.           
  987.           Group4Options
  988.           Tag  =  293  (125)
  989.           Type = LONG
  990.           N    = 1
  991.           
  992.           See Compression=4.   This  field is  made up  of a set of 32 flag
  993.           bits.   Unused bits are expected to be 0.  Bit 0 is the low-order
  994.           bit.   It is probably not safe to try to read the file if any bit
  995.           of this  field is  set that  you don't know the meaning of.  Gray
  996.           scale and color coding schemes are under study, and will be added
  997.           when finalized.
  998.           
  999.           For 2-D  coding, each  strip is  encoded as if it were a separate
  1000.           image.   In particular, each strip begins on a byte boundary; and
  1001.           the coding  for the first row of a strip is encoded independently
  1002.           of the  previous row,  using horizontal codes, as if the previous
  1003.           row is  entirely white.   Each strip ends with the 24-bit end-of-
  1004.           facsimile block (EOFB).
  1005.           
  1006.           Bit 0     is unused.
  1007.           Bit 1     is 1 if uncompressed mode is used.
  1008.           
  1009.           Default is  0, for  basic 2-dimensional  binary compression.  See
  1010.           also Compression.
  1011.           
  1012.           
  1013.           Document Storage and Retrieval Fields
  1014.           
  1015.           
  1016.           These fields  may be  useful for  document storage  and retrieval
  1017.           applications.   They are  not recommended  for use in interchange
  1018.           with desktop publishing applications.
  1019.           
  1020.           DocumentName
  1021.           Tag  = 269  (10D)
  1022.           Type = ASCII
  1023.           
  1024.           The name of the document from which this image was scanned.
  1025.           
  1026.           See also PageName.
  1027.           
  1028.           
  1029.           PageName
  1030.           Tag  = 285  (11D)
  1031.           Type = ASCII
  1032.           
  1033.           The name of the page from which this image was scanned.
  1034.           
  1035.           See also DocumentName.
  1036.           
  1037.           No default.
  1038.           
  1039.           PageNumber
  1040.           Tag  = 297  (129)
  1041.           Type = SHORT
  1042.           N    = 2
  1043.           
  1044.           This tag is used to specify page numbers of a multiple page (e.g.
  1045.           facsimile) document.   Two SHORT values are specified.  The first
  1046.           value is the page number; the second value is the total number of
  1047.           pages in the document.
  1048.           
  1049.           Note that  pages need  not appear  in numerical order.  The first
  1050.           page is 0 (zero).
  1051.           
  1052.           No default.
  1053.           
  1054.           
  1055.           XPosition
  1056.           Tag  = 286  (11E)
  1057.           Type = RATIONAL
  1058.           
  1059.           The X  offset of  the left side of the image, with respect to the
  1060.           left side of the page, in ResolutionUnits.
  1061.           
  1062.           No default.  See also YPosition.
  1063.           
  1064.           
  1065.           YPosition
  1066.           Tag  = 287  (11F)
  1067.           Type = RATIONAL
  1068.           
  1069.           The Y  offset of the top of the image, with respect to the top of
  1070.           the page, in ResolutionUnits.  In the TIFF coordinate scheme, the
  1071.           positive Y  direction  is  down,  so  that  YPosition  is  always
  1072.           positive.
  1073.           
  1074.           No default.  See also XPosition.
  1075.           
  1076.           
  1077.           No Longer Recommended
  1078.           
  1079.           
  1080.  
  1081.           These fields  are not  recommended except  perhaps for local use.
  1082.           They should  not be used for image interchange.  They have either
  1083.           been superseded  by other fields, have been found to have serious
  1084.           drawbacks, or are simply not as useful as once thought.  They may
  1085.           be dropped entirely from a future revision of the specification.
  1086.           
  1087.           CellLength
  1088.           Tag  = 265  (109)
  1089.           Type = SHORT
  1090.           N    = 1
  1091.           
  1092.           The length, in 1-bit samples, of the dithering/halftoning matrix.
  1093.           Assumes that Threshholding = 2.
  1094.           
  1095.           This field,  plus CellWidth  and Threshholding,  are  problematic
  1096.           because they  cannot safely be used to reverse-engineer grayscale
  1097.           image data  out of dithered/halftoned black-and-white data, which
  1098.           is their  only plausible  purpose.  The only "right" way to do it
  1099.           is to  not bother  with anything  like these  fields, and instead
  1100.           write  some  sophisticated  pattern-matching  software  that  can
  1101.           handle screen  angles that  are not  multiples of 45 degrees, and
  1102.           other such challenging dithered/halftoned data.
  1103.           
  1104.           So we  do not  recommend trying  to convert dithered or halftoned
  1105.           data into  grayscale data.   Dithered  and halftoned data require
  1106.           careful treatment  to avoid  "stretch marks," but it can be done.
  1107.           If you  want grayscale images, get them directly from the scanner
  1108.           or frame grabber or whatever.
  1109.           
  1110.           No default.  See also Threshholding.
  1111.           
  1112.           
  1113.           CellWidth
  1114.           Tag  = 264  (108)
  1115.           Type = SHORT
  1116.           N    = 1
  1117.           
  1118.           The width, in 1-bit samples, of the dithering/halftoning matrix.
  1119.           
  1120.           No default.   See  also Threshholding.    See  the  comments  for
  1121.           CellLength.
  1122.           
  1123.           
  1124.           FillOrder
  1125.           Tag  = 266  (10A)
  1126.           Type = SHORT
  1127.           N    = 1
  1128.           
  1129.           The order of data values within a byte.
  1130.           1 = most significant bits of the byte are filled first.  That is,
  1131.           data values  (or code  words) are  ordered from high order bit to
  1132.           low order bit within a byte.
  1133.           2 =  least significant  bits are  filled  first.    Since  little
  1134.           interest has  been expressed  in least-significant  fill order to
  1135.           date, and since it is easy and inexpensive for writers to reverse
  1136.           bit order (use a 256-byte lookup table), we recommend FillOrder=2
  1137.           for private (non-interchange) use only.
  1138.           
  1139.           Default is FillOrder = 1.
  1140.           
  1141.           
  1142.           FreeByteCounts
  1143.           Tag  = 289  (121)
  1144.           Type = LONG
  1145.           
  1146.           For each  "free block"   in  the file, the number of bytes in the
  1147.           block.
  1148.           
  1149.           TIFF  readers   can  ignore  FreeOffsets  and  FreeByteCounts  if
  1150.           present.
  1151.           
  1152.           FreeOffsets and  FreeByteCounts do  not constitute a remapping of
  1153.           the logical address space of the file.
  1154.           
  1155.           Since this  information can  be generated  by scanning  the IFDs,
  1156.           StripOffsets, and StripByteCounts, FreeByteCounts and FreeOffsets
  1157.           are not needed.
  1158.           
  1159.           In addition, it is not clear what should happen if FreeByteCounts
  1160.           and FreeOffsets exist in more than one IFD.
  1161.           
  1162.           See also FreeOffsets.
  1163.           
  1164.           
  1165.           FreeOffsets
  1166.           Tag  = 288  (120)
  1167.           Type = LONG
  1168.           
  1169.           For each "free block"  in the file, its byte offset.
  1170.           
  1171.           See also FreeByteCounts.
  1172.           
  1173.           
  1174.           MaxSampleValue
  1175.           Tag  = 281  (119)
  1176.           Type = SHORT
  1177.           N    = SamplesPerPixel
  1178.           
  1179.           The maximum  used sample  value.    For  example,  if  the  image
  1180.           consists of  6-bit data  low-order-justified  into  8-bit  bytes,
  1181.           MaxSampleValue will  be no  greater than 63. This is field is not
  1182.           to be  used to  affect the  visual appearance  of the  image when
  1183.           displayed.   Nor should  the values  of  this  field  affect  the
  1184.           interpretation of  any other  field.    Use  it  for  statistical
  1185.           purposes only.
  1186.           
  1187.           Default is 2**(BitsPerSample) - 1.
  1188.           
  1189.           
  1190.           MinSampleValue
  1191.           Tag  = 280  (118)
  1192.           Type = SHORT
  1193.           N    = SamplesPerPixel
  1194.           
  1195.           The minimum  used sample  value.  This field is not to be used to
  1196.           affect the  visual appearance  of the  image when displayed.  See
  1197.           the comments for MaxSampleValue.
  1198.           
  1199.           Default is 0.
  1200.           
  1201.           
  1202.           SubfileType
  1203.           Tag  = 255  (FF)
  1204.           Type = SHORT
  1205.           N    = 1
  1206.           
  1207.           A general  indication of  the kind  of data  that is contained in
  1208.           this subfile.  Currently defined values are:
  1209.           
  1210.           1 =  full  resolution  image  data - ImageWidth, ImageLength, and
  1211.           StripOffsets are required fields; and
  1212.           2 =  reduced resolution image data - ImageWidth, ImageLength, and
  1213.           StripOffsets are  required fields.   It is further assumed that a
  1214.           reduced resolution  image is  a reduced  version  of  the  entire
  1215.           extent of the corresponding full resolution data.
  1216.           3 =  single page  of a  multi-page image  (see the PageNumber tag
  1217.           description).
  1218.           
  1219.           Note that several image types can be found in a single TIFF file,
  1220.           with each subfile described by its own IFD.
  1221.           
  1222.           No default.
  1223.           
  1224.           Continued use  of this  field is not recommended.  Writers should
  1225.           instead use the new and more general NewSubfileType field.
  1226.           
  1227.           
  1228.           Orientation
  1229.           Tag  = 274 (112)
  1230.           Type = SHORT
  1231.           N    = 1
  1232.           
  1233.           1 =  The 0th  row represents the visual top of the image, and the
  1234.           0th column represents the visual left hand side.
  1235.           2 =  The 0th  row represents the visual top of the image, and the
  1236.           0th column represents the visual right hand side.
  1237.           3 =  The 0th  row represents  the visual bottom of the image, and
  1238.           the 0th column represents the visual right hand side.
  1239.           4 =  The 0th  row represents  the visual bottom of the image, and
  1240.           the 0th column represents the visual left hand side.
  1241.           5 =  The 0th  row represents  the visual  left hand  side of  the
  1242.           image, and the 0th column represents the visual top.
  1243.           6 =  The 0th  row represents  the visual  right hand  side of the
  1244.           image, and the 0th column represents the visual top.
  1245.           7 =  The 0th  row represents  the visual  right hand  side of the
  1246.           image, and the 0th column represents the visual bottom.
  1247.           8 =  The 0th  row represents  the visual  left hand  side of  the
  1248.           image, and the 0th column represents the visual bottom.
  1249.           
  1250.           Default is 1.
  1251.           
  1252.           This field is recommended for private (non-interchange) use only.
  1253.           It is extremely costly for most readers to perform image rotation
  1254.           "on the  fly," i.e.,  when importing  and printing;  and users of
  1255.           most  desktop  publishing  applications  do  not  expect  a  file
  1256.           imported by the application to be altered permanently in any way.
  1257.           
  1258.           
  1259.           Threshholding
  1260.           Tag  = 263  (107)
  1261.           Type = SHORT
  1262.           N    = 1
  1263.           
  1264.           1 = a bilevel "line art"  scan.  BitsPerSample must be 1.
  1265.           2 =  a "dithered"   scan, usually of continuous tone data such as
  1266.           photographs. BitsPerSample must be 1.
  1267.           3 = Error Diffused.
  1268.           
  1269.           Default is Threshholding = 1.  See also CellWidth, CellLength.
  1270.           4) Private Fields
  1271.           
  1272.           An organization  may wish to store information that is meaningful
  1273.           to only that organization in a TIFF file.  Tags numbered 32768 or
  1274.           higher  are  reserved  for  that  purpose.    Upon  request,  the
  1275.           administrator will  allocate and register a block of private tags
  1276.           for an  organization, to  avoid  possible  conflicts  with  other
  1277.           organizations.   Tags are  normally allocated  in blocks of five.
  1278.           If that is not enough, feel free to ask for more. You do not need
  1279.           to tell  the TIFF administrator or anyone else what you are going
  1280.           to use them for.
  1281.           
  1282.           Private enumerated  values  can  be  accommodated  in  a  similar
  1283.           fashion.   For example,  you may  wish to  experiment with  a new
  1284.           compression scheme  within TIFF.   Enumeration constants numbered
  1285.           32768 or  higher are  reserved for  private usage.  Upon request,
  1286.           the  administrator   will  allocate   and  register  a  block  of
  1287.           enumerated values  for a  particular field  (Compression, in  our
  1288.           example), to avoid possible conflicts.
  1289.           
  1290.           Tags and  values which  are allocated in the private number range
  1291.           are not  prohibited from  being included  in a future revision of
  1292.           this specification.   Several  such instances can be found in the
  1293.           TIFF specification.
  1294.           
  1295.           Do not  choose your  own tag  numbers.  If you do, it could cause
  1296.           serious problems some day.
  1297.           
  1298.           
  1299.           5) Image File Format Issues
  1300.           
  1301.           In the  quest to  give users no reason NOT to buy a product, some
  1302.           scanning and  image editing  applications overwhelm users with an
  1303.           incredible number  of "Save  As..." options.  Let's get rid of as
  1304.           many of  these as  we possibly  can.   For example, a single TIFF
  1305.           choice should  suffice once most major readers are supporting the
  1306.           three TIFF compression schemes; then writers can always compress.
  1307.           And given  TIFF's flexibility,  including private  tag and  image
  1308.           editing  support   features,  there  does  not  seem  to  be  any
  1309.           legitimate reason  for continuing  to  write  image  files  using
  1310.           proprietary formats.
  1311.           
  1312.           Along the  same lines,  there is no excuse for making a user have
  1313.           to know  the file  format of  a file  that is  to be  read by  an
  1314.           application program.   TIFF  files, as  well as  most other  file
  1315.           formats, contain  sufficient information  to enable  software  to
  1316.           automatically and  reliably distinguish  one type  of  file  from
  1317.           another.
  1318.           
  1319.           
  1320.           6) For Further Information
  1321.           
  1322.           Contact the  Aldus Developers' Desk for sample TIFF files, source
  1323.           code fragments,  and  a  list  of  features  that  are  currently
  1324.           supported in  Aldus products.   The Aldus Developers' Desk is the
  1325.           current "TIFF administrator."
  1326.           
  1327.           Various TIFF  related  aids  are  found  in  Microsoft's  Windows
  1328.           Developers Tookit for developers writing Windows applications.
  1329.           
  1330.           Finally, a  number of  scanner vendors are providing various TIFF
  1331.           services, such  as helping  to distribute  the TIFF specification
  1332.           and answering  TIFF questions.   Contact  the appropriate product
  1333.           manager or developer support service group.
  1334.           
  1335.  
  1336.           Appendix A:  Tag Structure Rationale
  1337.           
  1338.           
  1339.           A file  format is  defined by  both form (structure) and content.
  1340.           The content of TIFF consists of definitions of individual fields.
  1341.           It is therefore the content that we are ultimately interested in.
  1342.           The structure  merely tells  us how  to find the fields.  Yet the
  1343.           structure deserves  serious consideration for a number of reasons
  1344.           that are not at all obvious at first glance.  Since the structure
  1345.           described  herein   departs  significantly   from  several  other
  1346.           approaches, it may be useful to discuss the rationale behind it.
  1347.           
  1348.           The simplest,  most straightforward  structure for something like
  1349.           an image  file is  a positional  format.  In a positional scheme,
  1350.           the location  of the  data defines  what the  data  means.    For
  1351.           example, the  field for  "number of  rows" might  begin  at  byte
  1352.           offset 30 in the image file.
  1353.           
  1354.           This approach  is simple and easy to implement and is perfect for
  1355.           static environments.   But  if a  significant amount  of  ongoing
  1356.           change must  be accommodated,  subtle problems  begin to  appear.
  1357.           For example,  suppose that  a field  must be superseded by a new,
  1358.           more general  field.  You could bump a version number to flag the
  1359.           change.   Then  new  software  has  no  problem  doing  something
  1360.           sensible with  old data, and all old software will reject the new
  1361.           data, even  software that  didn't care about the old field.  This
  1362.           may seem like no more than a minor annoyance at first glance, but
  1363.           causing old  software to  break more  often than  it would really
  1364.           need to  can be very costly and, inevitably, causes much gnashing
  1365.           of teeth among customers.
  1366.           
  1367.           Furthermore, it  can be  avoided.   One approach  is to  store  a
  1368.           "valid" flag  bit for each field.  Now you don't have to bump the
  1369.           version number,  as long  as you  can put the new field somewhere
  1370.           that doesn't  disturb any  of the  old fields.  Old software that
  1371.           didn't care about that old field anyway can continue to function.
  1372.           (Old software  that did  care will of course have to give up, but
  1373.           this is an unavoidable price to be paid for the sake of progress,
  1374.           barring total omniscience.)
  1375.           
  1376.           Another problem  that crops  up frequently is that certain fields
  1377.           are likely  to make  sense only  if  other  fields  have  certain
  1378.           values.   This is not such a serious problem in practice; it just
  1379.           makes things  more confusing.   Nevertheless,  we note  that  the
  1380.           "valid" flag bits described in the previous paragraph can help to
  1381.           clarify the situation.
  1382.           
  1383.           Field-dumping  programs   can  be  very  helpful  for  diagnostic
  1384.           purposes.   A desirable  characteristic of such a program is that
  1385.           it doesn't  have to  know much  about what  it is  dumping.    In
  1386.           particular, it would be nice if the program could dump ASCII data
  1387.           in ASCII  format, integer  data in  integer format,  and  so  on,
  1388.           without having  to teach  the program  about new  fields all  the
  1389.           time.   So maybe  we should  add a  "data type"  component to our
  1390.           fields, plus  information on  how long  the field is, so that our
  1391.           dump program can walk through the fields without knowing what the
  1392.           fields "mean."
  1393.           
  1394.           But note  that if we add one more component to each field, namely
  1395.           a tag  that tells  what the field means, we can dispense with the
  1396.           "valid" flag  bits, and  we can  also avoid  wasting space on the
  1397.           non-valid fields in the file.  Simple image creation applications
  1398.           can write out several fields and be done.
  1399.           
  1400.           We have  now derived  the essentials  of a  tag-based image  file
  1401.           format.
  1402.           
  1403.           Finally, a  caveat.  A tag based scheme cannot guarantee painless
  1404.           growth.   But is  does provide  a useful  tool to  assist in  the
  1405.           process.
  1406.           
  1407.  
  1408.           Appendix B:  Data Compression - Scheme 2
  1409.           
  1410.           
  1411.           Abstract
  1412.           
  1413.           This document  describes a  method for  compressing bilevel  data
  1414.           that is  based on  the CCITT  Group 3  1D  facsimile  compression
  1415.           scheme.
  1416.           
  1417.           
  1418.           References
  1419.           
  1420.           1.   "Standardization of Group 3 facsimile apparatus for document
  1421.           transmission," Recommendation  T.4, Volume  VII, Fascicle  VII.3,
  1422.           Terminal Equipment  and Protocols  for  Telematic  Services,  The
  1423.           International  Telegraph  and  Telephone  Consultative  Committee
  1424.           (CCITT), Geneva, 1985, pages 16 through 31.
  1425.           2.   "Facsimile Coding  Schemes and  Coding Control Functions for
  1426.           Group 4  Facsimile Apparatus,"  Recommendation T.6,  Volume  VII,
  1427.           Fascicle VII.3,  Terminal Equipment  and Protocols  for Telematic
  1428.           Services, The  International Telegraph and Telephone Consultative
  1429.           Committee (CCITT), Geneva, 1985, pages 40 through 48.
  1430.           
  1431.           We do  not believe that these documents are necessary in order to
  1432.           implement Compression=2.   We  have included  (verbatim  in  most
  1433.           places) all the pertinent information in this Appendix.  However,
  1434.           if you  wish to  order the  documents, you  can  write  to  ANSI,
  1435.           Attention: Sales,  1430 Broadway, New York, N.Y., 10018.  Ask for
  1436.           the publication listed above -it contains both Recommendation T.4
  1437.           and T.6.
  1438.           
  1439.           
  1440.           Relationship to the CCITT Specifications
  1441.           
  1442.           The  CCITT   Group  3   and  Group   4  specifications   describe
  1443.           communications protocols for a particular class of devices.  They
  1444.           are not  by themselves sufficient to describe a disk data format.
  1445.           Fortunately, however,  the CCITT  coding schemes  can be  readily
  1446.           adapted to this different environment.  The following is one such
  1447.           adaptation.   Most of  the language  is copied  directly from the
  1448.           CCITT specifications.
  1449.           
  1450.           
  1451.           Coding Scheme
  1452.           
  1453.           A line  (row) of  data is composed of a series of variable length
  1454.           code words.  Each code word represents a run length of either all
  1455.           white or  all black.   (Actually,  more than one code word may be
  1456.           required to  code a  given run,  in a  manner  described  below.)
  1457.           White runs and black runs alternate.
  1458.           
  1459.           In order  to ensure  that the  receiver (decompressor)  maintains
  1460.           color synchronization, all data lines will begin with a white run
  1461.           length code  word set.   If  the actual  scan line  begins with a
  1462.           black run,  a white  run length  of zero  will be sent (written).
  1463.           Black or  white run  lengths are  defined by  the code  words  in
  1464.           Tables 1  and 2.   The  code words are of two types:  Terminating
  1465.           code  words   and  Make-up  code  words.    Each  run  length  is
  1466.           represented by  zero or  more  Make-up  code  words  followed  by
  1467.           exactly one Terminating code word.
  1468.           
  1469.           Run lengths  in the  range of  0 to  63 pels (pixels) are encoded
  1470.           with their appropriate Terminating code word.  Note that there is
  1471.           a different list of code words for black and white run lengths.
  1472.           
  1473.           Run lengths in the range of 64 to 2623 (2560+63) pels are encoded
  1474.           first by  the Make-up  code word representing the run length that
  1475.           is nearest  to, not  longer than,  that required.   This  is then
  1476.           followed by the Terminating code word representing the difference
  1477.           between the required run length and the run length represented by
  1478.           the Make-up code.
  1479.           
  1480.           Run lengths  in the range of lengths longer than or equal to 2624
  1481.           pels are  coded first  by the  Make-up code  of  2560.    If  the
  1482.           remaining part  of the run (after the first Make-up code of 2560)
  1483.           is 2560  pels or  greater, additional Make-up code(s) of 2560 are
  1484.           issued until the remaining part of the run becomes less than 2560
  1485.           pels.   Then  the  remaining  part  of  the  run  is  encoded  by
  1486.           Terminating code  or  by  Make-up  code  plus  Terminating  code,
  1487.           according to the range mentioned above.
  1488.           
  1489.           It is  considered an  unrecoverable error  if the  sum of the run
  1490.           lengths for  a line  does not  equal the  value of the ImageWidth
  1491.           field.
  1492.           
  1493.           New rows always begin on the next available byte boundary.
  1494.           
  1495.           No EOL  code words  are used.   No fill bits are used, except for
  1496.           the ignored  bits at  the end  of the last byte of a row.  RTC is
  1497.           not used.
  1498.           
  1499.           
  1500.           Table 1/T.4  Terminating codes
  1501.           
  1502.           
  1503.           White          Black
  1504.            run Code  run Code
  1505.           length    word length    word
  1506.            ----     ---- ------    ----
  1507.           
  1508.            0   00110101   0   0000110111
  1509.            1   000111     1   010
  1510.            2   0111  2   11
  1511.            3   1000  3   10
  1512.            4   1011  4   011
  1513.            5   1100  5   0011
  1514.            6   1110  6   0010
  1515.            7   1111  7   00011
  1516.            8   10011      8   000101
  1517.            9   10100      9   000100
  1518.           10   00111     10   0000100
  1519.           11   01000     11   0000101
  1520.           12   001000    12   0000111
  1521.           13   000011    13   00000100
  1522.           14   110100    14   00000111
  1523.           15   110101    15   000011000
  1524.           16   101010    16   0000010111
  1525.           17   101011    17   0000011000
  1526.           18   0100111   18   0000001000
  1527.           19   0001100   19   00001100111
  1528.           20   0001000   20   00001101000
  1529.           21   0010111   21   00001101100
  1530.           22   0000011   22   00000110111
  1531.           23   0000100   23   00000101000
  1532.           24   0101000   24   00000010111
  1533.           25   0101011   25   00000011000
  1534.           26   0010011   26   000011001010
  1535.           27   0100100   27   000011001011
  1536.           28   0011000   28   000011001100
  1537.           29   00000010  29   000011001101
  1538.           30   00000011  30   000001101000
  1539.           31   00011010  31   000001101001
  1540.           32   00011011  32   000001101010
  1541.           33   00010010  33   000001101011
  1542.           34   00010011  34   000011010010
  1543.           35   00010100  35   000011010011
  1544.           36   00010101  36   000011010100
  1545.           37   00010110  37   000011010101
  1546.           38   00010111  38   000011010110
  1547.           39   00101000  39   000011010111
  1548.           40   00101001  40   000001101100
  1549.           41   00101010  41   000001101101
  1550.           42   00101011  42   000011011010
  1551.           43   00101100  43   000011011011
  1552.           44   00101101  44   000001010100
  1553.           45   00000100  45   000001010101
  1554.           46   00000101  46   000001010110
  1555.           47   00001010  47   000001010111
  1556.           48   00001011  48   000001100100
  1557.           49   01010010  49   000001100101
  1558.           50   01010011  50   000001010010
  1559.           51   01010100  51   000001010011
  1560.           52   01010101  52   000000100100
  1561.           53   00100100  53   000000110111
  1562.           54   00100101  54   000000111000
  1563.           55   01011000  55   000000100111
  1564.           56   01011001  56   000000101000
  1565.           57   01011010  57   000001011000
  1566.           58   01011011  58   000001011001
  1567.           59   01001010  59   000000101011
  1568.           60   01001011  60   000000101100
  1569.           61   00110010  61   000001011010
  1570.           62   00110011  62   000001100110
  1571.           63   00110100  63   000001100111   
  1572.           
  1573.           
  1574.           
  1575.           
  1576.           Table 2/T.4  Make-up codes
  1577.           
  1578.           White          Black
  1579.            run Code  run Code
  1580.           length    word      length    word
  1581.           ------    ---- ------    ----
  1582.           
  1583.             64 11011       64 0000001111
  1584.            128 10010      128 000011001000
  1585.            192 010111     192 000011001001
  1586.            256 0110111    256 000001011011
  1587.            320 00110110   320 000000110011
  1588.            384 00110111   384 000000110100
  1589.            448 01100100   448 000000110101
  1590.            512 01100101   512 0000001101100
  1591.            576 01101000   576 0000001101101
  1592.            640 01100111   640 0000001001010
  1593.            704 011001100  704 0000001001011
  1594.            768 011001101  768 0000001001100
  1595.            832 011010010  832 0000001001101
  1596.            896 011010011  896 0000001110010
  1597.            960 011010100  960 0000001110011
  1598.           1024 011010101 1024 0000001110100
  1599.           1088 011010110 1088 0000001110101
  1600.           1152 011010111 1152 0000001110110
  1601.           1216 011011000 1216 0000001110111
  1602.           1280 011011001 1280 0000001010010
  1603.           1344 011011010 1344 0000001010011
  1604.           1408 011011011 1408 0000001010100
  1605.           1472 010011000 1472 0000001010101
  1606.           1536 010011001 1536 0000001011010
  1607.           1600 010011010 1600 0000001011011
  1608.           1664 011000    1664 0000001100100
  1609.           1728 010011011 1728 0000001100101
  1610.            EOL 000000000001    EOL 000000000001
  1611.           
  1612.           
  1613.           
  1614.           
  1615.           Additional make-up codes
  1616.           
  1617.           White
  1618.           and
  1619.           Black     Make-up
  1620.           run  code
  1621.           length    word
  1622.           ------    ----
  1623.           
  1624.           1792 00000001000
  1625.           1856 00000001100
  1626.           1920 00000001101
  1627.           1984 000000010010
  1628.           2048 000000010011
  1629.           2112 000000010100
  1630.           2176 000000010101
  1631.           2240 000000010110
  1632.           2304 000000010111
  1633.           2368 000000011100
  1634.           2432 000000011101
  1635.           2496 000000011110
  1636.           2560 000000011111
  1637.           
  1638.  
  1639.           Appendix C: Data Compression - Scheme 32773 -
  1640.           "PackBits"
  1641.           
  1642.           
  1643.           Abstract
  1644.           
  1645.           This document  describes a  simple compression scheme for bilevel
  1646.           scanned and paint type files.
  1647.           
  1648.           
  1649.           Motivation
  1650.           
  1651.           The TIFF  specification defines  a number of compression schemes.
  1652.           Compression type  1 is  really no  compression, other  than basic
  1653.           pixel  packing.     Compression   type  2,   based  on  CCITT  1D
  1654.           compression,  is   powerful,  but   not  trivial   to  implement.
  1655.           Compression type  5 is  typically very effective for most bilevel
  1656.           images, as  well as  many deeper images such as palette color and
  1657.           grayscale images, but is also not trivial to implement.  PackBits
  1658.           is a simple but often effective alternative.
  1659.           
  1660.           
  1661.           Description
  1662.           
  1663.           Several good schemes were already in use in various settings.  We
  1664.           somewhat arbitrarily picked the Macintosh PackBits scheme.  It is
  1665.           byte oriented,  so there  is no problem with word alignment.  And
  1666.           it has a good worst case behavior (at most 1 extra byte for every
  1667.           128 input  bytes).    For  Macintosh  users,  there  are  toolbox
  1668.           utilities PackBits  and UnPackBits that will do the work for you,
  1669.           but it is easy to implement your own routines.
  1670.           
  1671.           A pseudo code fragment to unpack might look like this:
  1672.           
  1673.           Loop  until  you  get  the  number  of  unpacked  bytes  you  are
  1674.           expecting:
  1675.                Read the next source byte into n.
  1676.                If n is between 0 and 127 inclusive, copy the next n+1 bytes
  1677.           literally.
  1678.                Else if  n is  between -127  and -1 inclusive, copy the next
  1679.           byte -n+1 times.
  1680.                Else if n is 128, noop.
  1681.           Endloop
  1682.           
  1683.           In the  inverse routine,  it's best to encode a 2-byte repeat run
  1684.           as a replicate run except when preceded and followed by a literal
  1685.           run, in  which case it's best to merge the three into one literal
  1686.           run.  Always encode 3-byte repeats as replicate runs.
  1687.           
  1688.           So that's the algorithm.  Here are some other rules:
  1689.           
  1690.           o    Each row  must be packed separately.  Do not compress across
  1691.           row boundaries.
  1692.  
  1693.           o    The number  of uncompressed  bytes per  row is defined to be
  1694.           (ImageWidth +  7) / 8.  If the uncompressed bitmap is required to
  1695.           have an  even number  of bytes  per row,  decompress  into  word-
  1696.           aligned buffers.
  1697.           o    If a  run is  larger  than  128  bytes,  simply  encode  the
  1698.           remainder of the run as one or more additional replicate runs.
  1699.           
  1700.           When  PackBits   data  is  uncompressed,  the  result  should  be
  1701.           interpreted as per compression type 1 (no compression).
  1702.           
  1703.  
  1704.           Appendix D
  1705.           
  1706.           
  1707.           Appendix D  has been  deleted.   It formerly contained guidelines
  1708.           for passing  TIFF files on the Microsoft Windows Clipboard.  This
  1709.           was judged to not be a good idea, in light of the ever-increasing
  1710.           size of  scanned images.   Applications are instead encouraged to
  1711.           employ file-based  mechanisms to  exchange  TIFF  data.    Aldus-
  1712.           PageMaker, for  example, implements  a "File  Place"  command  to
  1713.           allow TIFF files to be imported.
  1714.           
  1715.  
  1716.           Appendix E:  Numerical List of TIFF Tags
  1717.           
  1718.           
  1719.           NewSubfileType
  1720.           Tag  =  254  (FE)
  1721.           Type = LONG
  1722.           N    = 1
  1723.           
  1724.           SubfileType
  1725.           Tag  = 255  (FF)
  1726.           Type = SHORT
  1727.           N    = 1
  1728.           
  1729.           ImageWidth
  1730.           Tag  = 256  (100)
  1731.           Type = SHORT or LONG
  1732.           N    = 1
  1733.           
  1734.           ImageLength
  1735.           Tag  = 257  (101)
  1736.           Type = SHORT or LONG
  1737.           N    = 1
  1738.           
  1739.           BitsPerSample
  1740.           Tag  = 258  (102)
  1741.           Type = SHORT
  1742.           N    = SamplesPerPixel
  1743.           
  1744.           Compression
  1745.           Tag  = 259  (103)
  1746.           Type = SHORT
  1747.           N    = 1
  1748.           
  1749.           PhotometricInterpretation
  1750.           Tag  = 262  (106)
  1751.           Type = SHORT
  1752.           N    = 1
  1753.           
  1754.           
  1755.           Threshholding
  1756.           Tag  = 263  (107)
  1757.           Type = SHORT
  1758.           N    = 1
  1759.           
  1760.           CellWidth
  1761.           Tag  = 264  (108)
  1762.           Type = SHORT
  1763.           N    = 1
  1764.           
  1765.           CellLength
  1766.           Tag  = 265  (109)
  1767.           Type = SHORT
  1768.           N    = 1
  1769.           
  1770.           FillOrder
  1771.           Tag  = 266  (10A)
  1772.           Type = SHORT
  1773.           N    = 1
  1774.           
  1775.           DocumentName
  1776.           Tag  = 269  (10D)
  1777.           Type = ASCII
  1778.           
  1779.           ImageDescription
  1780.           Tag  = 270 (10E)
  1781.           Type = ASCII
  1782.           
  1783.           Make
  1784.           Tag  = 271  (10F)
  1785.           Type = ASCII
  1786.           
  1787.           Model
  1788.           Tag  = 272  (110)
  1789.           Type = ASCII
  1790.           
  1791.           StripOffsets
  1792.           Tag  = 273  (111)
  1793.           Type = SHORT or LONG
  1794.           N    = StripsPerImage for PlanarConfiguration equal to 1.
  1795.                = SamplesPerPixel  * StripsPerImage  for PlanarConfiguration
  1796.           equal to 2
  1797.           
  1798.           Orientation
  1799.           Tag  = 274 (112)
  1800.           Type = SHORT
  1801.           N    = 1
  1802.           
  1803.           SamplesPerPixel
  1804.           Tag  = 277  (115)
  1805.           Type = SHORT
  1806.           N    = 1
  1807.           
  1808.           RowsPerStrip
  1809.           Tag  = 278  (116)
  1810.           Type = SHORT or LONG
  1811.           N    = 1
  1812.           
  1813.           StripByteCounts
  1814.           Tag  = 279  (117)
  1815.           Type = LONG or SHORT
  1816.           N    = StripsPerImage for PlanarConfiguration equal to 1.
  1817.                = SamplesPerPixel  * StripsPerImage  for PlanarConfiguration
  1818.           equal to 2.
  1819.           
  1820.           MinSampleValue
  1821.           Tag  = 280  (118)
  1822.           Type = SHORT
  1823.           N    = SamplesPerPixel
  1824.           
  1825.           MaxSampleValue
  1826.           Tag  = 281  (119)
  1827.           Type = SHORT
  1828.           N    = SamplesPerPixel
  1829.           
  1830.           XResolution
  1831.           Tag  = 282  (11A)
  1832.           Type = RATIONAL
  1833.           N    = 1
  1834.           
  1835.           YResolution
  1836.           Tag  = 283  (11B)
  1837.           Type = RATIONAL
  1838.           N    = 1
  1839.           
  1840.           PlanarConfiguration
  1841.           Tag  = 284  (11C)
  1842.           Type = SHORT
  1843.           N    = 1
  1844.           
  1845.           PageName
  1846.           Tag  = 285  (11D)
  1847.           Type = ASCII
  1848.           
  1849.           
  1850.           XPosition
  1851.           Tag  = 286  (11E)
  1852.           Type = RATIONAL
  1853.           
  1854.           YPosition
  1855.           Tag  = 287  (11F)
  1856.           Type = RATIONAL
  1857.           
  1858.           FreeOffsets
  1859.           Tag  = 288  (120)
  1860.           Type = LONG
  1861.           
  1862.           FreeByteCounts
  1863.           Tag  = 289  (121)
  1864.           Type = LONG
  1865.           
  1866.           GrayResponseUnit
  1867.           Tag  = 290 (122)
  1868.           Type = SHORT
  1869.           N    = 1
  1870.           
  1871.           GrayResponseCurve
  1872.           Tag  = 291 (123)
  1873.           Type = SHORT
  1874.           N    = 2**BitsPerSample
  1875.           
  1876.           Group3Options
  1877.           Tag  = 292  (124)
  1878.           Type = LONG
  1879.           N    = 1
  1880.           
  1881.           Group4Options
  1882.           Tag  =  293  (125)
  1883.           Type = LONG
  1884.           N    = 1
  1885.           
  1886.           ResolutionUnit
  1887.           Tag  = 296 (128)
  1888.           Type = SHORT
  1889.           N    = 1
  1890.           
  1891.           PageNumber
  1892.           Tag  = 297  (129)
  1893.           Type = SHORT
  1894.           N    = 2
  1895.           
  1896.           ColorResponseCurves
  1897.           Tag  = 301 (12D)
  1898.           Type = SHORT
  1899.           N    = 3 * (2**BitsPerSample)
  1900.           
  1901.           Software
  1902.           Tag  = 305  (131)
  1903.           Type = ASCII
  1904.           
  1905.           DateTime
  1906.           Tag  = 306  (132)
  1907.           Type = ASCII
  1908.           N    = 20
  1909.           
  1910.           Artist
  1911.           Tag  = 315  (13B)
  1912.           Type = ASCII
  1913.           
  1914.           HostComputer
  1915.           Tag  = 316  (13C)
  1916.           Type = ASCII
  1917.           
  1918.           Predictor
  1919.           Tag  = 317 (13D)
  1920.           Type = SHORT
  1921.           N    = 1
  1922.           
  1923.           WhitePoint
  1924.           Tag  = 318 (13E)
  1925.           Type = RATIONAL
  1926.           N    = 2
  1927.           
  1928.           PrimaryChromaticities
  1929.           Tag  = 319 (13F)
  1930.           Type = RATIONAL
  1931.           N    = 6
  1932.           
  1933.           ColorMap
  1934.           Tag  = 320 (140)
  1935.           Type = SHORT
  1936.           N    = 3 * (2**BitsPerSample)
  1937.           
  1938.  
  1939.           Appendix F:  Data Compression - Scheme 5 - LZW
  1940.           Compression
  1941.           
  1942.           
  1943.           Abstract
  1944.           
  1945.           This document describes an adaptive compression scheme for raster
  1946.           images.
  1947.           
  1948.           
  1949.           Reference
  1950.           
  1951.           Terry  A.   Welch,  "A   Technique  for   High  Performance  Data
  1952.           Compression",  IEEE   Computer,  vol.   17  no.  6  (June  1984).
  1953.           Describes the  basic Lempel-Ziv  & Welch  (LZW) algorithm.    The
  1954.           author's goal  in the  article is  to describe  a  hardware-based
  1955.           compressor that could be built into a disk controller or database
  1956.           engine, and  used on  all types  of data.   There  is no specific
  1957.           discussion of  raster images.    We  intend  to  give  sufficient
  1958.           information in  this Appendix so that the article is not required
  1959.           reading.
  1960.           
  1961.           
  1962.           Requirements
  1963.           
  1964.           A compression  scheme with  the following  characteristics should
  1965.           work well in a desktop publishing environment:
  1966.           
  1967.           o    Must work well for images of any bit depth, including images
  1968.           deeper than 8 bits per sample.
  1969.           o    Must be effective:  an average compression ratio of at least
  1970.           2:1 or  better.    And  it  must  have  a  reasonable  worst-case
  1971.           behavior, in case something really strange is thrown at it.
  1972.           o    Should  not  depend  on  small  variations  between  pixels.
  1973.           Palette color  images tend  to contain  abrupt changes  in  index
  1974.           values, due to common patterning and dithering techniques.  These
  1975.           abrupt changes  do tend to be repetitive, however, and the scheme
  1976.           should make use of this fact.
  1977.           o    For images  generated by  paint programs,  the scheme should
  1978.           not depend on a particular pattern width.  8x8 pixel patterns are
  1979.           common now, but we should not assume that this situation will not
  1980.           change.
  1981.           o    Must be  fast.   It should  not take  more than 5 seconds to
  1982.           decompress a  100K byte  grayscale image on a 68020- or 386-based
  1983.           computer.   Compression can  be slower,  but probably not by more
  1984.           than a factor of 2 or 3.
  1985.           o    The level  of implementation  complexity must be reasonable.
  1986.           We would like something that can be implemented in no more than a
  1987.           couple of  weeks  by  a competent  software  engineer  with  some
  1988.           experience  in   image  processing.     The   compiled  code  for
  1989.           compression and  decompression combined  should be  no more  than
  1990.           about 10K.
  1991.           o    Does not require floating point software or hardware.
  1992.           
  1993.           
  1994.           The following  sections describe  an algorithm based on the "LZW"
  1995.           (Lempel-Ziv & Welch) technique that meets the above requirements.
  1996.           In addition  meeting our  requirements,  LZW  has  the  following
  1997.           characteristics:
  1998.           
  1999.           o    LZW is fully reversible.  All information is preserved.  But
  2000.           if noise  or information  is removed  from an  image, perhaps  by
  2001.           smoothing or  zeroing some  low-order bitplanes,  LZW  compresses
  2002.           images to  a smaller  size.   Thus,   5-bit, 6-bit, or 7-bit data
  2003.           masquerading as  8-bit data  compresses better  than  true  8-bit
  2004.           data. Smooth  images also  compress better than noisy images, and
  2005.           simple images compress better than complex images.
  2006.           o    On a  68082- or  386-based computer,  LZW  software  can  be
  2007.           written to  compress at  between 30K  and 80K  bytes per  second,
  2008.           depending on image characteristics.  LZW decompression speeds are
  2009.           typically about 50K bytes per second.
  2010.           o    LZW works  well on  bilevel images,  too.   It always  beats
  2011.           PackBits,  and   generally  ties   CCITT  1D  (Modified  Huffman)
  2012.           compression, on our test images.  Tying CCITT 1D is impressive in
  2013.           that LZW  seems to be considerably faster than CCITT 1D, at least
  2014.           in our implementation.
  2015.           o    Our implementation is written in C, and compiles to about 2K
  2016.           bytes of object code each for the compressor and decompressor.
  2017.           o    One of  the nice  things about  LZW is that it is used quite
  2018.           widely in  other applications  such as  archival programs, and is
  2019.           therefore more of a known quantity.
  2020.           
  2021.           
  2022.           The Algorithm
  2023.           
  2024.           Each strip  is compressed  independently.   We strongly recommend
  2025.           that RowsPerStrip  be chosen  such that each strip contains about
  2026.           8K bytes  before compression.   We  want to keep the strips small
  2027.           enough so  that the  compressed and  uncompressed versions of the
  2028.           strip can  be kept entirely in memory even on small machines, but
  2029.           large enough to maintain nearly optimal compression ratios.
  2030.           
  2031.           The LZW  algorithm is  based on  a translation  table, or  string
  2032.           table, that  maps strings  of input  characters into  codes.  The
  2033.           TIFF implementation  uses variable-length  codes, with  a maximum
  2034.           code length of 12 bits.  This string table is different for every
  2035.           strip, and,  remarkably, does  not need to be kept around for the
  2036.           decompressor.     The  trick   is  to   make   the   decompressor
  2037.           automatically build  the same  table as is built when compressing
  2038.           the data.   We  use a  C-like pseudocode  to describe  the coding
  2039.           scheme:
  2040.           
  2041.                InitializeStringTable();
  2042.                WriteCode(ClearCode);
  2043.                Omega = the empty string;
  2044.                for each character in the strip {
  2045.                     K = GetNextCharacter();
  2046.                     if Omega+K is in the string table {
  2047.                          Omega = Omega+K;  /* string concatenation */
  2048.                     } else {
  2049.                          WriteCode (CodeFromString(Omega));
  2050.                          AddTableEntry(Omega+K);
  2051.                          Omega = K;
  2052.                     }
  2053.                } /* end of for loop */
  2054.                WriteCode (CodeFromString(Omega));
  2055.                WriteCode (EndOfInformation);
  2056.                     
  2057.           That's  it.    The  scheme  is  simple,  although  it  is  fairly
  2058.           challenging  to  implement  efficiently.    But  we  need  a  few
  2059.           explanations before we go on to decompression.
  2060.           
  2061.           The  "characters"   that  make  up  the  LZW  strings  are  bytes
  2062.           containing TIFF  uncompressed (Compression=1)  image data, in our
  2063.           implementation.   For example,  if BitsPerSample is 4, each 8-bit
  2064.           LZW character will contain two 4-bit pixels.  If BitsPerSample is
  2065.           16, each 16-bit pixel will span two 8-bit LZW characters.
  2066.           
  2067.           (It is  also possible to implement a version of LZW where the LZW
  2068.           character depth equals BitsPerSample, as was described by Draft 2
  2069.           of Revision  5.0.   But  there  is  a  major  problem  with  this
  2070.           approach.   If BitsPerSample  is greater  than 11, we can not use
  2071.           12-bit-maximum  codes,   so  that  the  resulting  LZW  table  is
  2072.           unacceptably large.   Fortunately,  due to the adaptive nature of
  2073.           LZW, we  do not  pay a  significant compression ratio penalty for
  2074.           combining several  pixels into  one byte before compressing.  For
  2075.           example, our  4-bit sample  images  compressed  about  3  percent
  2076.           worse, and  our 1-bit  images compressed  about 5 percent better.
  2077.           And it  is easier to write an LZW compressor that always uses the
  2078.           same character  depth than  it is  to write  one which can handle
  2079.           varying depths.)
  2080.           
  2081.           We can  now describe  some of the routine and variable references
  2082.           in our pseudocode:
  2083.           
  2084.           InitializeStringTable() initializes  the string  table to contain
  2085.           all possible  single-character strings.   There  are 256 of them,
  2086.           numbered 0 through 255, since our characters are bytes.
  2087.           
  2088.           WriteCode() writes  a code  to the output stream.  The first code
  2089.           written is a Clear code, which is defined to be code #256.
  2090.           
  2091.           Omega is our "prefix string."
  2092.           
  2093.           GetNextCharacter() retrieves  the next  character value  from the
  2094.           input stream.   This  will be number between 0 and 255, since our
  2095.           characters are bytes.
  2096.           
  2097.           The "+" signs indicate string concatenation.
  2098.           
  2099.           AddTableEntry() adds a table entry.  (InitializeStringTable() has
  2100.           already put  256 entries  in our table.  Each entry consists of a
  2101.           single-character string, and its associated code value, which is,
  2102.           in our  application, identical to the character itself.  That is,
  2103.           the 0th  entry in  our table  consists of  the string  <0>,  with
  2104.           corresponding code  value of  <0>, the  1st entry  in  the  table
  2105.           consists of the string <1>, with corresponding code value of <1>,
  2106.           ..., and  the 255th  entry in  our table  consists of  the string
  2107.           <255>, with  corresponding code  value of  <255>.)   So the first
  2108.           entry that  we add  to our  string table will be at position 256,
  2109.           right?   Well, not  quite, since  we will reserve code #256 for a
  2110.           special   "Clear"   code,   and   code   #257   for   a   special
  2111.           "EndOfInformation" code  that we will write out at the end of the
  2112.           strip.  So the first multiple-character entry added to the string
  2113.           table will be at position 258.
  2114.           
  2115.           Let's try  an example.   Suppose  we have  input data  that looks
  2116.           like:
  2117.           
  2118.           Pixel 0:  <7>
  2119.           Pixel 1:  <7>
  2120.           Pixel 2:  <7>
  2121.           Pixel 3:  <8>
  2122.           Pixel 4:  <8>
  2123.           Pixel 5:  <7>
  2124.           Pixel 6:  <7>
  2125.           Pixel 7:  <6>
  2126.           Pixel 8:  <6>
  2127.           
  2128.           First, we read Pixel 0 into K.  OmegaK is then simply <7>, since Omega is
  2129.           the empty string at this point.  Is the string <7> already in the
  2130.           string table?  Of course, since all single character strings were
  2131.           put in the table by InitializeStringTable().  So set Omega equal to
  2132.           <7>, and go to the top of the loop.
  2133.           
  2134.           Read Pixel 1 into K.  Does OmegaK (<7><7>) exist in the string table?
  2135.           No, so we get to do some real work.  We write the code associated
  2136.           with Omega to output (write <7> to output), and add OmegaK (<7><7>) to
  2137.           the table as entry 258.   Store K (<7>) into Omega.    Note  that
  2138.           although we have added the string consisting of Pixel 0 and Pixel
  2139.           1 to  the table, we "re-use" Pixel 1 as the beginning of the next
  2140.           string.
  2141.           
  2142.           Back at the top of the loop.  We read Pixel 2 into K.  Does OmegaK
  2143.           (<7><7>) exist  in the  string table?   Yes,  the entry  we  just
  2144.           added, entry 258, contains exactly <7><7>.  So we just add K onto
  2145.           the end of Omega, so that Omega is now <7><7>.
  2146.           
  2147.           Back at the top of the loop.  We read Pixel 3 into K.  Does OmegaK
  2148.           (<7><7><8>) exist  in the  string table?   No,  so write the code
  2149.           associated with Omega (<258>) to output, and add OmegaK to the table as
  2150.           entry 259.  Store K (<8>) into Omega.
  2151.           
  2152.           Back at the top of the loop.  We read Pixel 4 into K.  Does OmegaK
  2153.           (<8><8>) exist  in the  string table?   No,  so  write  the  code
  2154.           associated with Omega (<8>) to output, and add OmegaK to the table as
  2155.           entry 260.  Store K (<8>) into Omega.
  2156.           
  2157.           Continuing, we get the following results:
  2158.           
  2159.                After reading: We write to output: And add table entry:
  2160.                Pixel 0
  2161.                Pixel 1   <7>  258: <7><7>
  2162.                Pixel 2
  2163.                Pixel 3   <258>     259: <7><7><8>
  2164.                Pixel 4   <8>  260: <8><8>
  2165.                Pixel 5   <8>  261: <8><7>
  2166.                Pixel 6
  2167.                Pixel 7   <258>     262: <7><7><6>
  2168.                Pixel 8   <6>  263: <6><6>
  2169.           
  2170.           WriteCode() also  requires some  explanation.   The  output  code
  2171.           stream,  <7><258><8><8><258><6>...  in  our  example,  should  be
  2172.           written using as few bits as possible.  When we are just starting
  2173.           out, we  can use  9-bit codes, since our new string table entries
  2174.           are greater  than 255  but less  than 512.  But when we add table
  2175.           entry 512,  we must  switch to 10-bit codes.  Likewise, we switch
  2176.           to 11-bit  codes at  1024, and  12-bit codes  at 2048.   We  will
  2177.           somewhat arbitrarily limit ourselves to 12-bit codes, so that our
  2178.           table can  have at most 4096 entries.  If we push it any farther,
  2179.           tables tend to get too large.
  2180.           
  2181.           What happens  if we run out of room in our string table?  This is
  2182.           where the afore-mentioned Clear code comes in.  As soon as we use
  2183.           entry 4094, we write out a (12-bit) Clear code.   (If we wait any
  2184.           longer to  write the  Clear code,  the decompressor  might try to
  2185.           interpret the  Clear code  as a 13-bit code.)  At this point, the
  2186.           compressor re-initializes the string table and starts writing out
  2187.           9-bit codes again.
  2188.           
  2189.           Note that whenever you write a code and add a table entry, Omega is
  2190.           not left  empty.   It contains exactly one character.  Be careful
  2191.           not to  lose it  when you  write an end-of-table Clear code.  You
  2192.           can either write it out as a 12-bit code before writing the Clear
  2193.           code, in  which case  you will  want to  do it right after adding
  2194.           table entry  4093, or  after the  clear code  as  a  9-bit  code.
  2195.           Decompression gives the same result in either case.
  2196.           
  2197.           To make  things a  little simpler  for the  decompressor, we will
  2198.           require that  each strip  begins with a Clear code, and ends with
  2199.           an EndOfInformation code.
  2200.           
  2201.           Every LZW-compressed  strip must  begin on  a byte  boundary.  It
  2202.           need not  begin on  a word  boundary.   LZW compression codes are
  2203.           stored into  bytes in  high-to-low-order fashion, i.e., FillOrder
  2204.           is assumed  to be  1.  The compressed codes are written as bytes,
  2205.           not  words,  so  that  the  compressed  data  will  be  identical
  2206.           regardless of whether it is an "II" or "MM" file.
  2207.           
  2208.           Note that  the LZW string table is a continuously updated history
  2209.           of the  strings that  have been encountered in the data.  It thus
  2210.           reflects the characteristics of the data, providing a high degree
  2211.           of adaptability.
  2212.           
  2213.           
  2214.           LZW Decoding
  2215.           
  2216.           The procedure for decompression is a little more complicated, but
  2217.           still not too bad:
  2218.                     
  2219.                while ((Code = GetNextCode()) != EoiCode) {
  2220.                     if (Code == ClearCode) {
  2221.                          InitializeTable();
  2222.                          Code = GetNextCode();
  2223.                          if (Code == EoiCode)
  2224.                               break;
  2225.                          WriteString(StringFromCode(Code));
  2226.                          OldCode = Code;
  2227.                     }  /* end of ClearCode case */
  2228.           
  2229.                     else {
  2230.                          if (IsInTable(Code)) {
  2231.                               WriteString(StringFromCode(Code));
  2232.                               AddStringToTable(StringFromCode(OldCode)+
  2233.                 FirstChar(StringFromCode(Code)));
  2234.                               OldCode = Code;
  2235.                          } else {
  2236.                               OutString = StringFromCode(OldCode) +
  2237.                       FirstChar(StringFromCode(OldCode));
  2238.                               WriteString(OutString);
  2239.                               AddStringToTable(OutString);
  2240.                               OldCode = Code;
  2241.                          }
  2242.                     } /* end of not-ClearCode case */
  2243.                } /* end of while loop */
  2244.           
  2245.           The function  GetNextCode() retrieves the next code from the LZW-
  2246.           coded data.  It must keep track of bit boundaries.  It knows that
  2247.           the first code that it gets will be a 9-bit code.  We add a table
  2248.           entry each  time we get a code, so GetNextCode() must switch over
  2249.           to 10-bit codes as soon as string #511 is stored into the table.
  2250.           
  2251.           The function  StringFromCode() gets  the string associated with a
  2252.           particular code from the string table.
  2253.           
  2254.           The function  AddStringToTable() adds  a  string  to  the  string
  2255.           table.   The "+"  sign joining  the two  parts of the argument to
  2256.           AddStringToTable indicate string concatenation.
  2257.           
  2258.           StringFromCode() looks  up the  string associated  with  a  given
  2259.           code.
  2260.           
  2261.           WriteString() adds a string to the output stream.
  2262.           
  2263.           
  2264.           When SamplesPerPixel Is Greater Than 1
  2265.           
  2266.           We  have   so  far   described  the   compression  scheme  as  if
  2267.           SamplesPerPixel were  always 1,  as will  be  be  the  case  with
  2268.           palette color  and grayscale  images.  But what do we do with RGB
  2269.           image data?
  2270.           
  2271.           Tests on  our sample  images indicate  that the  LZW  compression
  2272.           ratio    is    nearly    identical    regardless    of    whether
  2273.           PlanarConfiguration=1 or  PlanarConfiguration=2, for  RGB images.
  2274.           So use  whichever configuration  you prefer,  and simply compress
  2275.           the bytes in the strip.
  2276.           
  2277.           It is  worth cautioning  that compression  ratios on our test RGB
  2278.           images were disappointing low: somewhere between 1.1 to 1 and 1.5
  2279.           to 1,  depending on the image.  Vendors are urged to do what they
  2280.           can to  remove as  much noise  from  their  images  as  possible.
  2281.           Preliminary tests  indicate that significantly better compression
  2282.           ratios are  possibl